<template>
  <div ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll';

  export default {
    name: "Scroll",
    data() {
      return {
        scroll: null
      }
    },
    props: {
      probeType: {
        type: Number,
        default(){
          return 3;
        }
      },
      pullUpLoad: {
        type: Boolean,
        default() {
          return false;
        }
      }
    },
    mounted() {
      // 1、创建BScroll对象
      this.scroll = new BScroll(this.$refs.wrapper, {
        click: true,   // 允许scroll标签有点击事件，button自带click，可以不设置也有效果
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad,
      });

      // 2、监听滚动位置，是否弹出向上标签
      if (this.probeType === 2 || this.probeType === 3) {
        this.scroll.on('scroll', (position) => {
          this.$emit('scroll', position)
        });
      }

      // 3、监听滚动到底部
      if (this.pullUpLoad){
        this.scroll.on('pullingUp', ()=>{
          this.$emit('pullingUp')
        })
      }
    },
    methods: {
      // 返回顶部
      scrollTo(x, y, time = 300) {
        this.scroll && this.scroll.scrollTo(x, y, time)
      },

      // 结束上拉加载
      finishPullUp(){
        this.scroll && this.scroll.finishPullUp()
      },

      // 让scroll重新计算高度
      refresh(){
        this.scroll && this.scroll.refresh()
      },

      // 获取better-scroll滑动y轴距离
      getScrollY(){
        return this.scroll ? this.scroll.y : 0
      }
    }
  }
</script>

<style scoped>
</style>
